package com.ctsi.ssdc.mapper;

import com.ctsi.ssdc.entity.Excel08VO.Excel15VO;
import com.ctsi.ssdc.entity.Excel15;
import com.ctsi.ssdc.entity.SocialRetailSales;
import com.ctsi.ssdc.entity.TradeAndServiceIndustry;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.domain.Pageable;

import java.util.Date;
import java.util.List;

/**
 * 15基础表贸易分行业(Excel15)表数据库访问层
 *
 * @author makejava
 * @since 2023-02-21 16:26:42
 */
@Mapper
public interface Excel15Mapper {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Excel15 queryById(Long id);

    /**
     * 查询指定行数据
     *
     * @param excel15  查询条件
     * @param pageable 分页对象
     * @return 对象列表s
     */
    List<Excel15> queryAllByLimit(@Param("excel15") Excel15 excel15, @Param("pageable") Pageable pageable);

    /**
     * 统计总行数
     *
     * @param excel15 查询条件
     * @return 总行数
     */
    long count(Excel15 excel15);

    /**
     * 新增数据
     *
     * @param excel15 实例对象
     * @return 影响行数
     */
    int insert(Excel15 excel15);

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param entities List<Excel15> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("list") List<Excel15> entities);

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<Excel15> 实例对象列表
     * @return 影响行数
     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常，请自行校验入参
     */
    int insertOrUpdateBatch(@Param("list") List<Excel15> entities);

    /**
     * 修改数据
     *
     * @param excel15 实例对象
     * @return 影响行数
     */
    int update(Excel15 excel15);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Long id);


    @Select("SELECT `name`,sectorname, " +
            "ROUND(currentMonth) as currentmonth,ROUND(previousMonth) as previousmonth, " +
            "ROUND(currentAccumulate) as currentaccumulate, ROUND(previousAccumulate) as previousaccumulate, " +
            "ROUND(monthOnMonth1,1) as monthonmonth1,ROUND(monthOnMonth2,1) as monthonmonth2 " +
            "FROM excel_15 " +
            "WHERE YEAR(statisticalTime) = #{year} " +
            "AND MONTH(statisticalTime) = #{month} " +
            "AND zdbj = 1 " +
            "ORDER BY currentAccumulate DESC")
    List<Excel15VO> keyInfo(@Param("year") String year,@Param("month") String month);

    /**
     * 批零住餐营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     * @param year
     * @param month
     * @return
     */
    @Select("SELECT ROUND(SUM(currentMonth)/10000,2) as thisMonth, ROUND(SUM(currentAccumulate)/10000,2) as accumulate,\n" +
            "ROUND(((SUM(currentAccumulate)/SUM(previousMonth))-1)*100,1) as monthOnMonth\n" +
            "FROM excel_15\n" +
            "WHERE YEAR(statisticalTime) = #{year}\n" +
            "AND MONTH(statisticalTime) = #{month}")
    TradeAndServiceIndustry getTurnoverTotal(@Param("year") String year, @Param("month") String month);

    /**
     * 批零住餐营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     * @param year
     * @param month
     * @return
     */
    @Select("SELECT ROUND(SUM(currentMonth)/10000,2) as thisMonth, ROUND(SUM(currentAccumulate)/10000,2) as accumulate,\n" +
            "ROUND(((SUM(currentAccumulate)/SUM(previousAccumulate))-1)*100,1) as monthOnMonth\n" +
            "FROM excel_15\n" +
            "WHERE YEAR(statisticalTime) = #{year}\n" +
            "AND MONTH(statisticalTime) = #{month}\n" +
            "AND sectorName = #{sectorName}")
    TradeAndServiceIndustry getTurnoverTotalBySectorName(@Param("year") String year,@Param("month") String month,@Param("sectorName") String sectorName);

    /**
     * 根据日期删除
     * @param date
     * @return 是否成功
     */
    @Delete("delete from excel_15 where statisticalTime = #{date};")
    boolean removeByDate(@Param("date") Date date);
}

